import types from '@/constants/types.js'

import router from '@/router/index.js'
import socket from '@/tools/socket.js'
import broadcast from '@/tools/broadcast.js'
import states from '@/store/state.js'


import {
    Notification
} from 'element-ui'; // 配置请求的默认值 iframe用?

const state = states.studentsState;

const getters = {
    loginData(state, getters, rootState) {
        return state.loginData;
    },
    examGroupId(state, getters, rootState) {
        return state.examGroupId;
    },
    positionId(state, getters, rootState) {
        return state.positionId;
    },
    bigDataAnalystSubmit(state, getters, rootState) {
        return state.bigDataAnalystSubmit
    }
}

const mutations = {
    [types.STUDENTS_LOGINOUT](state, value) {
        state.loginData = {};
        sessionStorage.removeItem('state');
        sessionStorage.removeItem('accessToken');
        router.push({
            path: '/LoginIndex',
            query: {
                redirect: '/LoginIndex'
            }
        });
        broadcast.emit('loginBtnClick')
    },
    [types.STUDENTS_LOGININ](state, value) {
        state.loginData = value;
        // console.log(states.rootState.routermess);
        let obj = JSON.parse(states.rootState.routermess);
        if (value && value.Type)
            switch (value.Type) {
                case 1: //学生
                    obj.role = 'students';
                    break; //老师
                case 2:
                    obj.role = 'teachers';
                    break;
                case 3: //教务
                    obj.role = 'dean';
                    break;
            }
        states.rootState.routermess = JSON.stringify(obj);
        Notification({
            title: "提示",
            message: "登录成功！",
            type: "success",
          duration:500
        });
        if (value && value.accessToken)
            sessionStorage.setItem('accessToken', value.accessToken);
    },
    // 消息推送的服务器地址
    [types.STUDENTS_SERVERURL](state, value) {
        state.serverUrl = value;
        socket.open(); //建立websocket连接
    },
    // 作答者分组群聊Id
    [types.EXAMGROUPID](state, value) {
        state.examGroupId = value;
    },
    // 作答者岗位Id
    [types.POSITIONID](state, value) {
        state.positionId = value;
    },
    // 作答者岗位Id
    [types.BIGDATAANALYST](state) {
        state.bigDataAnalystSubmit = true;
    },
}

export default {
    namespaced: true,
    state,
    getters,
    mutations
}